package cn.ffcs.uec.staffAndOutStaff.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;

import cn.ffcs.uec.staffAndOutStaff.component.bean.StaffAndOutStaffListboxBean;
import cn.ffcs.uec.staffAndOutStaff.manager.StaffAndOutStaffManager;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * 外包合同制合并数据
 * 
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年9月12日
 */
@Controller
@Scope("prototype")
public class StaffAndOutStaffListbox extends Div implements IdSpace {

    private static final long serialVersionUID = 4510146500091327919L;

    private static final String STAFF_AND_OUTSTAFF_ZUL = "/pages_nmg/staffAndOutStaff/comp/staffAndOutStaff_listbox.zul";

    private StaffAndOutStaffListboxBean bean = new StaffAndOutStaffListboxBean();

    private StaffAndOutStaff staffAndOutStaff = new StaffAndOutStaff();

    private StaffAndOutStaffManager staffAndOutStaffManager = (StaffAndOutStaffManager) ApplicationContextUtil
            .getBean("staffAndOutStaffManager");

    private String operType = null;

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public StaffAndOutStaffListbox() {
        Executions.createComponents(STAFF_AND_OUTSTAFF_ZUL, this, null);
        Components.wireVariables(this, bean);
        Components.addForwards(this, this, '$');
        this.addForward("onOrgLeaderEdit", this, "onOrgLeaderEditResponse");
        this.addForward("onDeptLeaderEdit", this, "onDeptLeaderEditResponse");
        this.addForward("onDeptGlbdef1Edit", this, "onDeptGlbdef1EditResponse");
    }

    /**
     * 界面初始化
     */
    public void onCreate() {
    }

    /**
     * 监听组织负责人编辑的事件
     * 类：uecOrganizationMainComposer
     * @param event
     */
    public void onOrgLeaderEditResponse(final ForwardEvent event) {
        if (event.getOrigin().getData() != null) {
            operType = (String) event.getOrigin().getData();
            onQuery();
        }
    }

    /**
     * 监听部门负责人编辑事件
     * 类：UecOrganizationDeptMainComposer
     * @param event
     */
    public void onDeptLeaderEditResponse(final ForwardEvent event) {
        if (event.getOrigin().getData() != null) {
            operType = (String) event.getOrigin().getData();
            onQuery();
        }
    }
    
   /**
    * 监听部门分管领导编辑事件
    * 类：UecOrganizationDeptMainComposer
    * @param event
    */
    public void onDeptGlbdef1EditResponse(final ForwardEvent event) {
        if (event.getOrigin().getData() != null) {
            operType = (String) event.getOrigin().getData();
            onQuery();
        }
    }

    /**
     * 选中事件
     */
    public void onSelectRequest() {
        if (this.bean.getStaffOutStaffListbox().getSelectedCount() > 0) {
            staffAndOutStaff = (StaffAndOutStaff) this.bean.getStaffOutStaffListbox().getSelectedItem().getValue();
            if (staffAndOutStaff != null) {
                Events.postEvent("onSelectStaffAndOutStaff", this, staffAndOutStaff);
            } else {
                Events.postEvent("onSelectStaffAndOutStaff", this, null);
            }
        }
    }

    /**
     * 查询按钮
     */
    public void onQuery() {
        if (!StrUtil.isEmpty(this.bean.getCode().getValue())) {
            staffAndOutStaff.setStaffCode(this.bean.getCode().getValue());
        }
        if (!StrUtil.isEmpty(this.bean.getName().getValue())) {
            staffAndOutStaff.setStaffName(this.bean.getName().getValue());
        }
        onQueryStaffAndOutStaff();
    }

    /**
     * 查询员工信息
     * 
     * @throws Exception
     *             异常
     */
    public void onQueryStaffAndOutStaff() {
        int activePage = bean.getStaffOutStaffListboxPaging().getActivePage();
        int pageSize = bean.getStaffOutStaffListboxPaging().getPageSize();
        PageInfo pageInfo = staffAndOutStaffManager.queryPageInfoByAllStaffIncumbentsVo(staffAndOutStaff, operType, activePage + 1,
                pageSize);
        ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
        this.bean.getStaffOutStaffListbox().setModel(list);
        this.bean.getStaffOutStaffListboxPaging().setTotalSize(pageInfo.getTotalCount());
    }

    /**
     * 翻页
     */
    public void onListboxPaging() {
        onQueryStaffAndOutStaff();
    }

    /**
     * 重置按钮
     */
    public void onReset() {
        this.bean.getName().setValue(null);
        this.bean.getCode().setValue(null);
        ListboxUtils.clearListbox(this.bean.getStaffOutStaffListbox());
        this.bean.getStaffOutStaffListboxPaging().setTotalSize(1);
        staffAndOutStaff = new StaffAndOutStaff();
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        staffAndOutStaff = null;
    }
}
